"""
主题: 在定义的时候初始化类的成员
问题: 你想在类被定义的时候就初始化一部分类的成员，而不是要等到实例被创建后。
提示 : 
"""

import operator

class StructTupleMeta(type):
    def __init__(cls, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for n, name in enumerate(cls._fields):
            setattr(cls, name, property(operator.itemgetter(n)))

class StructTuple(tuple, metaclass=StructTupleMeta):
    _fields = []
    def __new__(cls, *args):
        if len(args) != len(cls._fields):
            raise ValueError('{} arguments required'.format(len(cls._fields)))
        return super().__new__(cls,args)
    
class Stock(StructTuple):
    _fields = ['name', 'shares', 'price']

class Point(StructTuple):
    _fields = ['x', 'y']

def recipe1():
    s = Stock('ACME', 50, 91.1)

    print(f"{s = }")
    print(f"{s[0] = }")
    print(f"{s.name = }")

    try:
        s.shares = 23
    except AttributeError as e:
        print(e)  

def main():
    print('recipe1'.center(20, '*'))
    recipe1()

if __name__ == '__main__':
    main()           